import 'package:flutter/material.dart';
import 'package:proj_shop/constant/constant.dart';

typedef void ITextFieldCallBack(String content);

class AccountEditText extends StatefulWidget {
  final ITextFieldCallBack contentStrCallBack;

  const AccountEditText({Key key, this.contentStrCallBack}) : super(key: key);
  @override
  _AccountEditTextState createState() => _AccountEditTextState();
}

String _inputAccount = "";
bool _isShowDelete = false;

class _AccountEditTextState extends State<AccountEditText> {
  TextEditingController _controller = TextEditingController.fromValue(
    TextEditingValue(
      text: _inputAccount,
      selection: new TextSelection.fromPosition(
        TextPosition(
          affinity: TextAffinity.downstream,
          offset: _inputAccount.length,
        ),
      ),
    ),
  );

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Theme(
        data: ThemeData(
          highlightColor: Colors.transparent,
          splashColor: Colors.transparent,
        ), //去除水波纹效果
        child: TextField(
          controller: _controller,
          style: TextStyle(color: Color(0xff333333), fontSize: 14),
          keyboardType: TextInputType.text,
          maxLength: 20,
          maxLines: 1,
          decoration: buildInputDecoration(),
          onChanged: textFieldChanged,
        ),
      ),
    );
  }

  void textFieldChanged(str) {
    setState(() {
      print(str);
      _inputAccount = str;
      _isShowDelete = (_inputAccount.isNotEmpty);
      widget.contentStrCallBack(_inputAccount);
    });
  }

  InputDecoration buildInputDecoration() {
    return InputDecoration(
      counterText: "",
      hintText: "手机号或者邮箱",
      contentPadding: EdgeInsets.only(left: 0, top: 14, bottom: 14),
      hintStyle: TextStyle(color: Color(0xff8C8C8C), fontSize: 14),
      focusedBorder: UnderlineInputBorder(
        borderSide: BorderSide(color: Colors.orange),
      ),
      enabledBorder: UnderlineInputBorder(
        borderSide: BorderSide(color: Color(0xffDADADA)),
      ),
      fillColor: Colors.transparent,
      filled: true,
      suffixIcon: buildSuffixIcon(),
    );
  }

  Widget buildSuffixIcon() {
    return _isShowDelete
        ? IconButton(
            icon: Image.asset(
              Constant.ASSETS_IMG + 'icon_et_delete.png',
              width: 14.0,
              height: 14.0,
            ),
            onPressed: () {},
          )
        : Text("");
  }
}
